package mvc_generator.generator.car.struts

import mvc_generator.generator.MVCGeneratorGeneratorUtils
import mvc_generator.mVCGenerator.AssignedField
import mvc_generator.mVCGenerator.FormValidation
import mvc_generator.mVCGenerator.ModelMVC
import org.eclipse.xtext.generator.IFileSystemAccess
import mvc_generator.mVCGenerator.View

class ServletActionGenerator {

	def void generate(FormValidation fv, IFileSystemAccess fsa) {

		var ModelMVC modelMVCContainer = MVCGeneratorGeneratorUtils.findModelMVCContainer(fv);
		var CharSequence servletActionContent = '''''';

		servletActionContent = '''
				package servletAction;
			
				import javax.servlet.http.HttpServletRequest;
				import javax.servlet.http.HttpServletResponse;
				import org.apache.struts.Globals;
				import org.apache.struts.action.Action;
				import org.apache.struts.action.ActionForm;
				import org.apache.struts.action.ActionForward;
				import org.apache.struts.action.ActionMapping;
				import org.apache.struts.action.ActionMessage;
				import org.apache.struts.action.ActionMessages;
				«StrutsGenerator.domainJavaImport»
			
				public class «fv.validatedFormName.toFirstUpper»Action extends Action {
			
				public ActionForward execute(final ActionMapping pMapping, ActionForm pForm, 
											 final HttpServletRequest pRequest, final HttpServletResponse pResponse) {
				
				final «fv.validatedFormName.toFirstUpper»ValidationForm lForm = («fv.validatedFormName.toFirstUpper»ValidationForm) pForm;
				
					«FOR AssignedField assignedField : MVCGeneratorGeneratorUtils.getValidateForm(fv).assignedFields»
						final «MVCGeneratorGeneratorUtils.getSimpleTypeOfAssignement(assignedField).stringRepresentation» «assignedField.
				fieldName» = lForm.get«assignedField.fieldName.toFirstUpper»();
					«ENDFOR»
					
					
					/*
					 *  USAGE : return pMapping.findForward("<yourViewName>"); 
					 */
					
				}
			}		
		'''
		fsa.generateFile(
			modelMVCContainer.name + "Struts" + StrutsGenerator.servletActionPath + fv.validatedFormName + "Action.java",
			servletActionContent);

	}
}
